﻿namespace Standards.Web.Providers
{
    using System;
    using System.Data.Entity;
    using System.Linq;
    using System.Web.Security;

    using Standards.Data.EntityFramework;

    public class StandardsRoleProvider : RoleProvider
    {
        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override string ApplicationName { get; set; }

        public override void CreateRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            throw new NotImplementedException();
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            throw new NotImplementedException();
        }

        public override string[] GetAllRoles()
        {
            //TODO: dependency injection
            using (var unitOfWork = new StandardsUnitOfWork())
            {
                var roleNames = unitOfWork.Roles.GetAll().Select(r => r.Name);
                return roleNames.ToArray();
            }            
        }

        public override string[] GetRolesForUser(string username)
        {
            using (var unitOfWork = new StandardsUnitOfWork())
            {
                var user = unitOfWork.Users.GetAll().Include(u => u.Roles).FirstOrDefault(u => u.Name == username);
                return user == null ? Enumerable.Empty<string>().ToArray() : user.Roles.Select(r => r.Name).ToArray();
            }
        }

        public override string[] GetUsersInRole(string roleName)
        {
            using (var unitOfWork = new StandardsUnitOfWork())
            {
                var role = unitOfWork.Roles.GetAll().Include(r => r.Users).FirstOrDefault(r => r.Name == roleName);
                return role == null ? Enumerable.Empty<string>().ToArray() : role.Users.Select(u => u.Name).ToArray();
            }
        }

        public override bool IsUserInRole(string username, string roleName)
        {
            using (var unitOfWork = new StandardsUnitOfWork())
            {
                var user = unitOfWork.Users.GetAll().Include(u => u.Roles).FirstOrDefault(u => u.Name == username);
                if (user == null)
                {
                    return false;
                }

                var role = user.Roles.FirstOrDefault(r => r.Name == roleName);
                return role != null;
            }
        }

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override bool RoleExists(string roleName)
        {
            using (var unitOfWork = new StandardsUnitOfWork())
            {
                var role = unitOfWork.Roles.GetAll().FirstOrDefault(r => r.Name == roleName);
                return role != null;
            }
        }
    }
}